package com.vastdata.spring.jdbc.service;

import com.vastdata.spring.jdbc.dao.SampleDao;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@RequiredArgsConstructor
@Service
public class SampleService {

    private SampleDao sampleDao;

    private DataSourceTransactionManager transactionManager;

    public void testTransaction() {
        TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
        try {
            boolean execute = sampleDao.execute("", new String[]{});
            System.out.println(execute);
            transactionManager.commit(transaction);
        } catch (Exception e) {
            transactionManager.rollback(transaction);
        }
    }

}
